//
// @Author: 18574
// @Description:
// @File:  main
// @Version: 1.0.0
// @Date: 2022/4/3 17:16
//

package main

import "fmt"

func minArray(numbers []int) int {
	if len(numbers) == 0 {
		return -1
	}
	l, r := 0, len(numbers)-1
	for l < r {
		mid := (r-l)>>1 + l
		if numbers[mid] < numbers[r] {
			// 左边比右边小
			r = mid
		} else if numbers[mid] > numbers[r] {
			l = mid + 1
		} else {
			r--
		}
	}
	return numbers[l]
}
func minArrayV1(numbers []int) int {
	if len(numbers) == 0 {
		return -1
	}
	l, r := 0, len(numbers)-1
	mid := (r-l)>>1 + l
	for numbers[l] > numbers[r] {
		if r-l == 1 {
			mid = r
			break
		}
		mid = (r-l)>>1 + l
		if numbers[mid] >= numbers[l] {
			l = mid
		} else if numbers[mid] <= numbers[r] {
			r = mid
		}
	}
	return numbers[mid]
}

func minArrayV2(numbers []int) int {
	if len(numbers) == 0 {
		return 0
	}
	l, r := 0, len(numbers)-1
	for l < r {
		if l == r-1 {
			return r
		}
		mid := (r-l)>>1 + l
		if numbers[mid] > numbers[l] {
			l = mid
			continue
		}
		r = mid
	}
	return r
}

func main() {
	fmt.Println(minArrayV1([]int{3, 4, 5, 1, 2}))
	fmt.Println(minArray([]int{2, 2, 2, 0, 1}))
}
